package com.xiaoyu.lanling.widget.audio;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xiaoyu.base.app.AppContext;
import com.xiaoyu.base.app.GlobalUI;
import com.xiaoyu.lanling.R;

public class AudioRecordButton extends View {

    private boolean mHasPerformedLongClick;
    private boolean mIsRecording;

    private RxPermissions mRxPermissions;
    private AudioRecordGeastureListener mRecordGestureListener;

    public AudioRecordButton(Context context) {
        this(context, null);
    }

    public AudioRecordButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AudioRecordButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int y = (int) event.getY();

        if (mHasPerformedLongClick) {
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                    if (y < -50) {
                        if (mRecordGestureListener != null) {
                            mRecordGestureListener.onMoveToCancel();
                        }
                    } else {
                        if (mRecordGestureListener != null) {
                            mRecordGestureListener.onMoveToContinue();
                        }
                    }
                    return true;
                case MotionEvent.ACTION_CANCEL:
                    mHasPerformedLongClick = false;
                    if (mRecordGestureListener != null) {
                        mRecordGestureListener.onStopRecord(true);
                        mRecordGestureListener.onLongClickChanged(false);
                    }
                    return true;
                case MotionEvent.ACTION_UP:
                    mHasPerformedLongClick = false;
                    if (mIsRecording) {
                        if (y > -50) {
                            if (mRecordGestureListener != null) {
                                mRecordGestureListener.onStopRecord(true);
                                mRecordGestureListener.onLongClickChanged(false);
                            }
                        } else {
                            if (mRecordGestureListener != null) {
                                mRecordGestureListener.onStopRecord(false);
                                mRecordGestureListener.onLongClickChanged(false);
                            }
                        }
                        mIsRecording = false;
                    }
                    return true;
            }
        }
        mHasPerformedLongClick = false;
        return super.onTouchEvent(event);
    }

    @SuppressLint("CheckResult")
    public boolean performLongClick(boolean performedLongClick) {
        if (getContext() == null) {
            return false;
        }
        if (mHasPerformedLongClick != performedLongClick) {
            if (mRecordGestureListener != null) {
                mRecordGestureListener.onLongClickChanged(performedLongClick);
            }
        }
        mHasPerformedLongClick = performedLongClick;

        if (mRxPermissions == null) {
            mRxPermissions = new RxPermissions((FragmentActivity) getContext());
        }
        boolean isGranted = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;

        mRxPermissions.request(Manifest.permission.RECORD_AUDIO)
                .subscribe(granted -> {
                    if (granted) {
                        if (!isGranted) {
                            return;
                        }
                        if (mRecordGestureListener != null) {
                            mRecordGestureListener.onStartRecord();
                            mIsRecording = true;
                        }
                    } else {
                        GlobalUI.getInstance().showToast(AppContext.getString(R.string.permission_audio_record));
                    }
                });
        return true;
    }

    public boolean hasPerformedLongClick() {
        return mHasPerformedLongClick;
    }

    public interface AudioRecordGeastureListener {

        void onStartRecord();

        void onMoveToCancel();

        void onMoveToContinue();

        void onStopRecord(boolean save);

        void onLongClickChanged(boolean isLongClicked);
    }

    public void setRecordGestureListener(AudioRecordGeastureListener audioRecordGeastureListener) {
        this.mRecordGestureListener = audioRecordGeastureListener;
    }
}
